package com.zadtek.shopmall.other;

import com.zadtek.shopmall.bean.CityBean;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class AreaManger {
    private static List<CityBean> cityBeans;
    public interface OnArae{
        void onFetch(List<CityBean> cityBeans);
    }

    public static void fetch(final OnArae listener){
        if(cityBeans!=null && listener!=null){
            listener.onFetch(cityBeans);
        }else{
            NetTool.getApi().getAreaList().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<BaseResponse<List<CityBean>>>() {
                        @Override
                        public void onData(BaseResponse<List<CityBean>> value) {
                            if(value.getCode()==0 && value.getData()!=null){
                                cityBeans = value.getData().get(0).getSon();
                                if(listener!=null) listener.onFetch(cityBeans);
                            }
                        }
                    });
        }
    }

}
